<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Admin_home extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('session');
				$this->load->library('form_validation');
        $this->load->model('batch_model');
        $this->load->model('user_man_model');
        $this->load->model('lisensi_model');
    }

	
	/* function cek_login()
	{
		if($session->userdata->)
	} */
	
    function index() {
		if($this->session->userdata('id_hak') == 2 || $this->session->userdata('id_hak') == 1 || $this->session->userdata('id_hak') != ''){
			//get data batch
			$data['batch'] = $this->batch_model->get_all_batch();

			$data['title'] = 'Admin Home';
			$this->template->write_view('header', 'template/header', $data); 
			$this->template->write_view('content', 'home_admin', $data);
			$this->template->write_view('footer', 'template/footer', $data);
			$this->template->render();
		}
		else{
			redirect('login');
		}
		
    }

    function login() {
        $this->session->sess_destroy();
        $valid = false;
		
		
		//cek login
		$this->form_validation->set_rules('username', 'Username', 'required');
		$this->form_validation->set_rules('password', 'Password', 'required');
		$this->form_validation->set_message('required', 'Kolom %s harus diisi.');
						
		if($this->form_validation->run() == TRUE)
		{
			$users = $this->user_man_model->get_user();

			$id = $this->input->post('username');
			$password = $this->input->post('password');
			//kondisi pengecekan apakah username dan password yang dimasukkan telah sesuai dengan benar atau tidak
			foreach ($users->result() as $row) {
				if ($id == $row->USERNAME && md5($password) == $row->PASSWORD) {
					$valid = true;
					//setting session terhadap data user
					$newdata = array(
						'username' => $id,
						'password' => $password,
						'id_hak' => $row->ID_PRIVILAGE,
						'id_user' => $row->ID_USER,
						'id_alumni' => $row->ID_ALUMNI,
						'id_lisensi' => $row->ID_LISENSI
					);
					$this->session->set_userdata($newdata);
					break;
				}
			}//end foreach
			//apabila login telah sesuai dengan username dan password maka user akan masuk halaman utama
			if ($valid) {
				if ($this->session->userdata('id_hak') == 3) {
					redirect('alumni/alumni/preview_by_user');
				} else {
					if($this->session->userdata('id_hak') == 2)
					{
						$record_data = $this->lisensi_model->selectone($this->session->userdata('id_lisensi'));
						foreach ($record_data->result() as $lisensi)
						{
							$this->session->set_userdata('nama_lisensi', $lisensi->NAMA_LISENSI);
							$this->session->set_userdata('cp_lisensi', $lisensi->CP);
						}
					}
					redirect('admin_home');
				}
			}
			//apabila login tidak sesuai dengan username dan password maka user akan masuk halaman login
			else {
				$data['cek_form'] = 1;
				$data['title'] = 'Login';
				$this->template->write_view('content', 'login',$data);
				$this->template->render();
			}
		}
		else 
		{
			$data['title'] = 'Login';
			$this->template->write_view('content', 'login',$data);
			$this->template->render();
		} // end foreach
    }

//end if

    function logout() {
        $this->session->sess_destroy();
        redirect('login');
    }

    function backup() {
        $this->load->helper('download');
		$tanggal=date('d-m-Y');
		$namaFile='backup_database_simpel_'.$tanggal . '.sql.zip';
		$this->load->dbutil();
		$backup=& $this->dbutil->backup();
		force_download($namaFile, $backup);
    }

}

/* End of file deabakery.php */
/* Location: ./application/controllers/deabakery.php */
